package com.lyh.hkcloud.module.m1300090;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lyh.hkcloud.R;
import com.lyh.hkcloud.adapter.AutoCompleteTextViewAdapter;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.constants.HttpUrlComstants;
import com.lyh.hkcloud.module.m1300085.service.Service;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.SaveData;
import com.lyh.hkcloud.util.base.StringUtils;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * //{"dataIds":["1563469462613524482"],"data":[{"id":"1563469462613524482","num":"1"}],"maindata":{"field_6887600017549299712":"一期工业园办公楼131303","field_6887600017540911109":"Q1-B3-13F-1303"},"userid":"1465136442656292864","username":"温文亮"}
 *
 * @author: wenwenliang
 * @description: 更新位置资产信息
 * @date: 2023-11-27 14:01
 **/
public class UpdateExceptionMemo extends AppCompatActivity implements View.OnClickListener {

    private Service service;
    /**
     * 单号
     */
    private TextView tvInventoryNo;
    /**
     * 实物码
     */
    private TextView tvCardNumber;
    /**
     * 异常备注原因消息
     */
    private EditText tvExceptionMemo;


    private ImageView goBack;
    /**
     * 保存按钮
     */
    private Button btSave;
    /**
     * 取消按钮
     */
    private Button btCancel;

    /**
     * 当前实物码资产信息数据(每次打开页面查询)
     */
    private List<Map<String, Object>> faCardInfo = new ArrayList<>();
    /**
     * 代码提交信息提示组件
     */
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //调接口服务
        service = new Service(UpdateExceptionMemo.this);
        //加载布局
        setContentView(R.layout.activity_1300090_exception_memo);
        //去除页头
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        //组件初始化
        initAssembly();
    }

    @Override
    protected void onStart() {
        super.onStart();
        //赋值实物码
        Intent intent = getIntent();
        String cardNumber = intent.getStringExtra("cardNumber");
        String inventoryNo = intent.getStringExtra("inventoryNo");
        String exceptionMemo = intent.getStringExtra(BusinessConstants.EXCEPTION_MEMO);
        tvCardNumber.setText(cardNumber);
        tvInventoryNo.setText(inventoryNo);
        tvExceptionMemo.setText(exceptionMemo);
        //查询当前实物码资产信息数据并赋值到成员变量faCardInfo数组map中
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("psncode", cardNumber);
            jsonObject.put("facardcode", cardNumber);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_save:
                save();
                break;
            case R.id.go_back:
            case R.id.bt_cancel:
                finish();
                break;
        }

    }

    //组件初始化
    private void initAssembly() {
        //单号
        tvInventoryNo = findViewById(R.id.tv_inventoryNo);
        //实物码
        tvCardNumber = findViewById(R.id.tv_card_number);
        //保存按钮
        btSave = findViewById(R.id.bt_save);
        btSave.setOnClickListener(this);
        //内容
        tvExceptionMemo = findViewById(R.id.et_memo);
        //箭头返回
        goBack = findViewById(R.id.go_back);
        goBack.setOnClickListener(this);
        //取消
        btCancel = findViewById(R.id.bt_cancel);
        btCancel.setOnClickListener(this);
    }


    /**
     * 保存数据
     */
    protected void save() {

        String cardNumber = tvCardNumber.getText().toString();
        String inventoryNo = tvInventoryNo.getText().toString();
        String memo = tvExceptionMemo.getText().toString();

        /*参数校验*/
        if (StringUtils.isEmpty(cardNumber)) {
            Toast.makeText(this, "必须得实物码不存在", Toast.LENGTH_SHORT).show();
            return;
        }

        /*单号*/
        if (StringUtils.isEmpty(cardNumber)) {
            Toast.makeText(this, "必须得单号不存在", Toast.LENGTH_SHORT).show();
            return;
        }

        //禁用提交按钮
        btSave.setEnabled(false);

        //组装参数
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject();


            jsonObject.put("cardNumber", cardNumber);
            jsonObject.put("inventoryNo", inventoryNo);
            jsonObject.put("exceptionMemo", memo);

            Log.v("jsonObject资产信息提交", jsonObject.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }

        service.post(jsonObject, HttpUrlComstants.UPDATE_EXCEPTION_MEMO, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                /**
                 * {
                 *     "data": null,
                 *     "code": "0000",
                 *     "message": "请求成功。",
                 *     "now": "2024-05-06 10:37:51"
                 * }
                 */
                try {
                    String code = response.getString("code");
                    if ("0000".equals(code)) {
                        Toast.makeText(UpdateExceptionMemo.this, "修改成功！", Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(UpdateExceptionMemo.this, "修改失败！", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(UpdateExceptionMemo.this, "修改失败！", Toast.LENGTH_SHORT).show();

                }

                JSONObject jsonObject2 = new JSONObject();
                //盘点单号
                try {
                    jsonObject2.put("inventoryNo", inventoryNo);
                    jsonObject2.put("cardNumber", cardNumber);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                //同步资产信息
                service.post(jsonObject2, HttpUrlComstants.SYNC_FA_CARD, new JsonSuccessListener() {
                    @Override
                    public void onRespone(String msg, JSONObject response) {
                        String code = response.optString("code");
                        if ("0000".equals(code)) {
                            //
                            Toast.makeText(UpdateExceptionMemo.this, response.optString("message"), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(UpdateExceptionMemo.this, IndexActivity.class);
                            setResult(11, intent);
                            //退出当前页面
                            finish();
                        }
                    }
                }, new JsonFailuredListener() {
                    @Override
                    public void onRespone(String msg, JSONObject response) {
                        Log.e("error", response.optString("message"));
                    }
                });
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Toast.makeText(UpdateExceptionMemo.this, "修改失败:"+response.optString("message"), Toast.LENGTH_SHORT).show();
            }
        });

    }
            /**
             * 提示加载
             */
            public void showProgressDialog(String title, String message) {
                if (progressDialog == null) {
                    progressDialog = ProgressDialog.show(UpdateExceptionMemo.this, title, message, true, false);
                } else if (progressDialog.isShowing()) {
                    progressDialog.setTitle(title);
                    progressDialog.setMessage(message);
                }

                progressDialog.show();

            }

            /**
             * 隐藏提示加载
             */
            public void hideProgressDialog() {
                if (progressDialog != null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
    }